package cc.telecomdigital.tdstock.trading;

import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.Activity_Framework;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

import cc.telecomdigital.tdstock.activity.AccountLoginActivity;
import cc.telecomdigital.tdstock.trading.Services.TradeAppInfo;
import cc.telecomdigital.tdstock.trading.widget.AppDialogOK;
import cc.telecomdigital.tdstock.trading.widget.AppDialogOK.IAppDialogOK;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

public abstract class Trade_TemplateActivity extends Activity_Framework
{
	@Override
	protected void onResume()
	{
		super.onResume();
		if (TradeAppInfo.HasSessionTimeExpired() && !IsSpecialActivity())
		{
			AppDialogOK promptDialog;
			
			promptDialog = new AppDialogOK(this);
			promptDialog.SetTextContent(R.string.loginExpireText);
			promptDialog.show(new IAppDialogOK()
			{
				@Override
				public void OnOK_Click(DialogInterface iDialog)
				{
					SetToLoginActivity();
				}

				@Override
				public void OnDialogCancel(DialogInterface iDialog)
				{
					SetToLoginActivity();	
				}
				
			}, false);
			return;
		}
		
		// If it was shown before, bring it back
		if (progressShow)
			ShowWaitingDialog();
	}

	@Override
	protected void onPause()
	{
		HideKeyBoard();
		// Temporary Dismiss
		DismissWaitingDialog_Sub();
//		if (!IsSpecialActivity())
//			TradeAppInfo.UpdateLastExitTradingTime();
		super.onPause();
	}

	protected AppDialogOK ShowInfoBox(String iContent)
	{
		AppDialogOK infoDialog;
		
		infoDialog = new AppDialogOK(this);
		infoDialog.SetTextContent(iContent);
		infoDialog.SetButtonText(R.string.okText);
		infoDialog.show(null, true);
		
		return infoDialog;
	}

	private ProgressDialog	waitingDialog;
	private boolean		progressShow	= false;

	protected void ShowWaitingDialog()
	{
			//modified by haibo ,to avoid  android.view.WindowManager$BadTokenException
		try
		{
			View  currentActivityView  =  getWindow().getDecorView().findViewById(android.R.id.content);
			if(currentActivityView == null  || currentActivityView.getVisibility() != View.VISIBLE ) {
				return;
			}
		
		
			if (waitingDialog == null)
			{
				waitingDialog = new ProgressDialog(this);
				waitingDialog.setIndeterminate(true);
				waitingDialog.setCancelable(false);
			}
			progressShow = true;
			waitingDialog.setMessage(getString(R.string.waitingText));
			waitingDialog.show();
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}		
	}
		
	protected void DismissWaitingDialog()
	{
		DismissWaitingDialog_Sub();
		progressShow = false;
	}

	protected void DismissWaitingDialog_Background()
	{
		progressShow = false;
	}

	private void DismissWaitingDialog_Sub()
	{
		if (waitingDialog == null)
			return;

		if (waitingDialog.isShowing())
			waitingDialog.dismiss();
	}

	@Override
	protected void onDestroy()
	{
		DismissWaitingDialog();
		super.onDestroy();
	}

//	// Remember only Activity has the preference table
//	private static final String	_preferenceTable	= Trade_AppConstant._preferenceTable;
//
//	protected SharedPreferences GetAppPreference()
//	{
//		return getSharedPreferences(_preferenceTable, Activity.MODE_PRIVATE);
//	}

//	protected void SetTradeingVersion(String iVersion)
//	{
//		SharedPreferences.Editor pref;
//		String  setValue;
//		
//		pref = GetAppPreference().edit();
//		setValue = iVersion;
//		if ((setValue == null) || setValue.equals(""))
//			setValue = "";
//			
//		pref.putString(Trade_AppConstant.versionNamePref, setValue);
//
//		pref.commit();
//		
//	}	
//	
//	protected void SetUserName(String iUserName)
//	{
//		SharedPreferences.Editor pref;
//		String  setValue;
//		
//		pref = GetAppPreference().edit();
//		if ((iUserName == null) || iUserName.equals(""))
//			setValue = "";
//		else
//			setValue = EncryptString(iUserName);	
//		
//		if (TradeAppInfo.IsDemoAccount())
//			pref.putString(Trade_AppConstant.demoUserNamePref, setValue);
//		else
//		 	pref.putString(Trade_AppConstant.userNamePref, setValue);
//		pref.commit();
//		
//	}
//	
//	// Most Communication would require this
//	protected String GetUserName()
//	{		
//		SharedPreferences pref;
//	
//		if (TradeAppInfo.IsTradingRegistered() == false)
//			return "";
//		
//		pref = GetAppPreference();
//
//		String userName;
//		
//		if (TradeAppInfo.IsDemoAccount())
//			userName =  pref.getString(Trade_AppConstant.demoUserNamePref, "");
//		else
//		 	userName = pref.getString(Trade_AppConstant.userNamePref, "");
//		
//		if (userName.equals(""))
//			return userName;
//		
//		return DecryptString(userName);
//	}

	//Password that save locally in the ram area, and a sharing ingo
	//static private String userPassword = null;
//	protected void SetUserPassword(String iUserPassword)
//	{		
//		if ((iUserPassword == null) || iUserPassword.equals(""))
//		{				
//			userPassword = "";
//			return;
//		}
//		
//		//Encrypted
//		userPassword = EncryptString(iUserPassword);
//	}
	
	// Most Communication would require this
//	protected String GetUserPassword()
//	{
//		if ((userPassword == null) || userPassword.equals(""))
//			return "";
//		
//		return DecryptString(userPassword);		
//	}

//	// Using AES for encryption
//	protected String EncryptString(String iString)
//	{
//		String encryptData;
//
//		try
//		{
//			encryptData = EncryptionAlg.encrypt(
//					Trade_AppConstant.encryptPairKey, iString);
//		}
//		catch (Exception iE)
//		{
//			//ShowInfoBox(getString(R.string.systemErrorText));
//			return "";
//		}
//		return encryptData;
//	}
//
//	protected String DecryptString(String iString)
//	{
//		String dencryptData;
//
//		try
//		{
//			dencryptData = EncryptionAlg.decrypt(
//					Trade_AppConstant.encryptPairKey, iString);
//		}
//		catch (Exception iE)
//		{
//			//ShowInfoBox(getString(R.string.systemErrorText));
//			return "";
//		}
//		return dencryptData;
//	}

	// Set Keyboard to focus
	protected View.OnFocusChangeListener	_keyboardPopListener	= new View.OnFocusChangeListener()
														{
															@Override
															public void onFocusChange(
																	View iViewObj,
																	boolean hasFocus)
															{
																if (hasFocus)
																	ShowKeyBoard(iViewObj);
															}
														};

	protected void ShowKeyBoard(View iViewObj)
	{
		InputMethodManager mgr;

		if (iViewObj == null)
			return;

		mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		mgr.showSoftInput(iViewObj, InputMethodManager.SHOW_FORCED);
	}

	protected void HideKeyBoard()
	{
		InputMethodManager mgr;
		View viewObj;

		try
		{
			viewObj = getCurrentFocus();
			if (viewObj == null)
				return;
			
			mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			if (mgr == null)
				return;
			
			if (mgr.isActive())
				mgr.hideSoftInputFromWindow(viewObj.getWindowToken(), 0);
		}
		catch(Exception ex)
		{
			TDStockLog.e(GetIdentity(), "HideKeyBoard: " + ex.toString());
		}
	}

	protected boolean ShowTimeExpiredBox()
	{
		AppDialogOK promptDialog;

		// Reset the system memory
		TradeAppInfo.SetTradingRegistration(false);
		// Prepare the dialog box
		promptDialog = new AppDialogOK(this);
		promptDialog.SetTextContent(R.string.loginExpireText);
		promptDialog.SetButtonText(R.string.okText);
		promptDialog.show(new AppDialogOK.IAppDialogOK()
		{
			@Override
			public void OnDialogCancel(DialogInterface iDialog)
			{
				SetToLoginActivity();
			}

			@Override
			public void OnOK_Click(DialogInterface iDialog)
			{
				SetToLoginActivity();
			}
		}, true);
		return true;
	}
	
	protected void SetToLoginActivity()
	{		
		TradeAppInfo.ResetAllInfo();
		// Back to the login display					
		//Should remove our others
		Class <? extends Activity>  previous;
		
		previous = TradeAppInfo.GetPrimaryEntryClass();
		if(previous != null)
			RemoveAfterActivitysFromList(previous);	
	
		//If it is not delay mode, we should allow user to use again
		if (!TradeAppInfo.IsAllowDemoOnly(appHost.GetLSClient_ServicePlan()))
			TradeAppInfo.SetAsDemoAccount(false);
		
		SwitchFreeActivity(Trade_LoginActivity.class);		
	}	
	
	private boolean IsSpecialActivity()
	{
		if (getClass().equals(Trade_LoginActivity.class))
			return true;
		
		return false;
	}
	
	protected void SetToAppLoginActivity()
	{
		// Back to the login display					
		//Should remove our others
		Class <? extends Activity>  previous;
		
		previous = TradeAppInfo.GetPrimaryEntryClass();
		if(previous != null)
			RemoveAfterActivitysFromList(previous);	
		
		SwitchFreeActivity(AccountLoginActivity.class);
		//This is to request the AccountLoginActivity to show the prompt message
		AccountLoginActivity._isAnotherLogonClientAppear = true;
	}
	
}
